<?php
/**
 * @title 173. 二叉搜索树迭代器
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     public $val = null;
 *     public $left = null;
 *     public $right = null;
 *     function __construct($val = 0, $left = null, $right = null) {
 *         $this->val = $val;
 *         $this->left = $left;
 *         $this->right = $right;
 *     }
 * }
 */
class BSTIterator {
    /**
     * @param TreeNode $root
     */
    function __construct($root) {
        /**
         * @since 2023-12-27 TreeNode变成中序数组
         * @since 2023-12-27 为方便数组弹出，改成right,root,left，不用Array_shift
         */
        $this->nodeArray = $this->dfs($root);
    }

    /**
     * @title 返回right-root-left数组
     * @author start2004
     * @since 2023-12-27 Created
     *
     * @param TreeNode $root
     * @return Int[]
     */
    function dfs($root){
        /**
         * @since 2023-12-27 数字元素的集合
         */
        $numArray = [];

        /**
         * @since 2023-12-27 右子树
         */
        if($root->right !== null){
            $numArray = array_merge($numArray, $this->dfs($root->right));
        } else {}

        /**
         * @since 2023-12-27 根节点
         */
        $numArray[] = $root->val;

        /**
         * @since 2023-12-27 左子树
         */
        if($root->left !== null) {
            $numArray = array_merge($numArray, $this->dfs($root->left));
        } else {}

        /**
         * @return Int[]
         */
        return $numArray;
    }

    /**
     * @return Integer
     */
    function next() {
        /**
         * @return Integer
         */
        return array_pop($this->nodeArray);
    }

    /**
     * @return Boolean
     */
    function hasNext() {
        /**
         * @return Boolean
         */
        return !empty($this->nodeArray);
    }
}

/**
 * Your BSTIterator object will be instantiated and called as such:
 * $obj = BSTIterator($root);
 * $ret_1 = $obj->next();
 * $ret_2 = $obj->hasNext();
 */


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.27_13.php
 */
$datas = [

];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
